/**
 * Copyright (c) 2016-2100 yunclouds
 */
package com.yunqian.web;

import com.yunqian.common.CurrentUser;
import com.yunqian.jwt.JwtUser;
import com.yunqian.redot.constant.BusinessType;
import com.yunqian.redot.constant.Operation;
import com.yunqian.redot.constant.OperationMenus;
import com.yunqian.redot.domain.Contents;
import com.yunqian.redot.domain.Information;
import com.yunqian.redot.domain.dto.InformationDto;
import com.yunqian.redot.service.ContentsService;
import com.yunqian.redot.service.InformationService;
import com.yunqian.redot.service.OperationLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Describe: 咨询文章控制器
 *
 * @author zsy
 * 2018-09-29 15:54:34
 */
@RestController
@RequestMapping("/api/information")
public class InformationController {

    @Autowired
    private InformationService informationService;
    @Autowired
    private ContentsService contentsService;
    @Autowired
    private OperationLogService operationLogService;

    /**
     * 获取分页
     *
     * @param params
     * @return
     */
    @PostMapping("/pagination")
    public Page<Information> pagination(@RequestBody Map<String, Object> params) {
        return informationService.pagination(params);
    }

    /**
     * 获取所有咨讯（显示）
     *
     * @return
     */
    @GetMapping("/all")
    public Map<String, Object> pagination() {
        Map<String, Object> map = new HashMap<>(16);
        List<Information> informationList = informationService.getAllByStatus();
        map.put("informationList", informationList);
        return map;
    }

    /**
     * 添加咨讯
     *
     */
    @PostMapping
    public void save(@RequestBody InformationDto dto, @CurrentUser JwtUser user) {
        informationService.save(dto, user.getId());
    }

    /**
     * 隐藏/显示
     *
     * @param id         待更新id
     * @param modifyData 待更新的数据
     * @return
     */
    @PutMapping("/{id}")
    public Information update(@PathVariable("id") Long id, @RequestBody Map<String, Object> modifyData, @CurrentUser JwtUser user) {
        Information information = informationService.update(id, modifyData);
        if(information.getStatus() == 2){
            operationLogService.save(OperationMenus.NEWS_LIST, Operation.SHOW, user.getId());
        }else {
            operationLogService.save(OperationMenus.NEWS_LIST, Operation.HIDE, user.getId());
        }
        return information;
    }

    /**
     * 编辑
     * @return
     */
    @PutMapping
    public void update(@RequestBody InformationDto dto, @CurrentUser JwtUser user) {
        informationService.update(dto, user.getId());
    }

    /**
     * 获取详情
     *
     * @param id 要查询的id
     * @return
     */
    @GetMapping("/{id}")
    public InformationDto detail(@PathVariable("id") Long id) {
        Information information = informationService.findStrictOne(id);
        List<Contents> contentsList = contentsService.findAllByRelateBusinessAndRelateId(BusinessType.INFORMATION.getCode(), information.getId());
        InformationDto dto = new InformationDto();
        dto.setInformation(information);
        dto.setContentsList(contentsList);
        return dto;
    }


}
